from django.shortcuts import render, redirect
from django.urls import reverse

from client.api import createDailyForm
from client.models import Client as ClientModel, ClientManager, ClientVisit
from sdk.views import sendTextMsgApi
from user.models import User


# https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwcb3168f059eec7d6&redirect_uri=https%3A%2F%2Fwecom.qhduhu.com%2Fuser%2Flogin&response_type=code&scope=snsapi_base&agentid=1000002&state=client#wechat_redirect
# https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwcb3168f059eec7d6&redirect_uri=https%3A%2F%2Fwecom.qhduhu.com%2Fuser%2Flogin&response_type=code&scope=snsapi_base&agentid=1000002&state=dash#wechat_redirect
# https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwcb3168f059eec7d6&redirect_uri=https%3A%2F%2Fwecom.qhduhu.com%2Fuser%2Flogin&response_type=code&scope=snsapi_base&agentid=1000002&state=user#wechat_redirect


def IndexView(request):
    userid = request.session.get('userid', 0)
    if userid == 0:
        return redirect('user:login')
    else:
        user = User.objects.get(id=userid)
    # 我的客户
    clientManagerLst = ClientManager.objects.filter(user=user)
    clientLst = []
    for clientManager in clientManagerLst:
        clientLst.append(clientManager.client)
    return render(request, "client/index.html", locals())


def ClientSearchView(request):
    userid = request.session.get('userid', 0)
    if userid == 0:
        return redirect('user:login')
    else:
        user = User.objects.get(id=userid)
    if request.method == "GET":
        return render(request, "client/clientsearch.html", locals())
    else:
        name = request.POST.get("name")
        try:
            client = ClientModel.objects.get(name=name)
            # return redirect(reverse('client:clientIndex', client_id=client.id))
            # return redirect(reverse('client:clientIndex', kwargs={'client_id': client.id}))
            url = reverse('client:clientIndex') + "?client_id=" + str(client.id)
            return redirect(url)
        except ClientModel.DoesNotExist:
            errormsg = "未找到该客户"
            return render(request, "client/clientsearch.html", locals())


def ClientAddView(request):
    userid = request.session.get('userid', 0)
    if userid == 0:
        return redirect('user:login')
    else:
        user = User.objects.get(id=userid)
    name = request.GET.get("name")
    if request.method == "GET":
        return render(request, "client/clientadd.html", locals())
    else:
        try:
            client = ClientModel()
            client.name = name
            client.save()
            clientManager = ClientManager()
            clientManager.client = client
            clientManager.user = user
            clientManager.save()
            url = reverse('client:clientIndex') + "?client_id=" + str(client.id)
            return redirect(url)
        except Exception as e:
            print(e)
            errormsg = "添加失败"
            return render(request, "client/clientadd.html", locals())


def ClientIndexView(request):
    userid = request.session.get('userid', 0)
    if userid == 0:
        return redirect('user:login')
    else:
        user = User.objects.get(id=userid)
    if request.method == "GET":
        client_id = request.GET.get("client_id")
        client = ClientModel.objects.get(id=client_id)
        managerLst = ClientManager.objects.filter(client=client)
        managerFlag = False
        for manager in managerLst:
            if manager.user == user:
                managerFlag = True
        clientVisitLst = ClientVisit.objects.filter(client=client)
        visitFlag = False
        for visit in clientVisitLst:
            if visit.status == 0:
                visitFlag = True
    return render(request, "client/clientindex.html", locals())


def ClientBindView(request):
    userid = request.session.get('userid', 0)
    if userid == 0:
        return redirect('user:login')
    else:
        user = User.objects.get(id=userid)
    if request.method == "GET":
        client_id = request.GET.get("client_id")
        client = ClientModel.objects.get(id=client_id)
        clientManager = ClientManager()
        clientManager.client = client
        clientManager.user = user
        clientManager.save()
        url = reverse('client:clientIndex') + "?client_id=" + str(client.id)
        return redirect(url)


def VisitCreateView(request):
    userid = request.session.get('userid', 0)
    if userid == 0:
        return redirect('user:login')
    else:
        user = User.objects.get(id=userid)
    if request.method == "GET":
        client_id = request.GET.get("client_id")
        client = ClientModel.objects.get(id=client_id)
        return render(request, "client/visitcreate.html", locals())
    else:
        client_id = request.GET.get("client_id")
        client = ClientModel.objects.get(id=client_id)
        kind = request.POST.get("kind")
        print(kind)
        receiver = request.POST.get("receiver")
        print(receiver)
        userids = []
        if receiver == 1:
            userids.append(user.wxid)
        else:
            for manager in ClientManager.objects.filter(client=client):
                userids.append(manager.user.wxid)
        dailyReq = createDailyForm(client.name,userids)
        dailyCode = dailyReq["code"]
        if dailyCode == 0:
            dailyData = dailyReq["data"]
            formid = dailyData["formid"]
            visit = ClientVisit()
            visit.kind = kind
            visit.client = client
            visit.formid = formid
            visit.creater = user.name
            visit.save()
            sendTextMsgApi(user.wxid, "拜访工单创建成功")
            url = reverse('client:clientIndex') + "?client_id=" + str(client.id)
            return redirect(url)
        else:
            errormsg = "创建失败"
            return render(request, "client/visitcreate.html", locals())


